package smartcampus.scjava.controller.second;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import smartcampus.scjava.POJO.DO.AcademicArchives;
import smartcampus.scjava.POJO.DO.Student;
import smartcampus.scjava.POJO.DTO.AcademicArchivesDTO;
import smartcampus.scjava.custom.ResultVO;
import smartcampus.scjava.service.AcademicArchivesService;
import smartcampus.scjava.service.StudentService;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@RequestMapping("/student")
@ApiOperation("个人信息详情表")
@RestController
@CrossOrigin
public class StudentController {
    @Autowired
    StudentService studentService;

    @Autowired
    AcademicArchivesService academicArchivesService;

    @Autowired
    TuitionFeeController tuitionFeeController;


    @PostMapping("/insertStudent")
    @ApiOperation("新增一条学生个人信息")
    public ResultVO<?> insertStudent(@RequestBody Student student) {
        //1.先查询该学号是否存在
        String selectStuid = selectStuid(student.getStuid());
        //如果没有则新增一条学生个人信息
        if (null == selectStuid) {
            Date now1 = new Date();
            student.setCreateTime(now1);
            student.setFinishischool(0);
            student.setAtschool(1);
            Boolean a = studentService.insertByStuid(student);
            if(a){
            //新增学生个人信息时，自动创建学费信息
            tuitionFeeController.addTuition(student.getStuid());
            return ResultVO.success("添加成功");}
            else {
                return ResultVO.fail("新增失败");
            }
        } else {
            return ResultVO.fail("学号已存在，请重新新增");
        }
    }

    @PostMapping("/delByStuId")
    @ApiOperation("根据学号删除学生信息")
    public ResultVO<?> delByStuId(@RequestBody Student student) {
        AcademicArchivesDTO selectObject = academicArchivesService.selectByStuId(student.getStuid());
        if (null == selectObject) {
            String stuid = selectStuid(student.getStuid());
            if (null != stuid) {
                Boolean a = studentService.delByStuId(stuid);
                if (a) {
                    return ResultVO.success("删除成功");
                } else {
                    return ResultVO.fail("删除失败");
                }

            }
            return ResultVO.fail("没有该学号，请重新输入学号");
        } else {
            return ResultVO.fail("该学生信息在学籍表中存在，请删除学籍表信息后再试");
        }
    }


    @PostMapping("/updateStudent")
    @ApiOperation("修改一条学生个人信息")
    public ResultVO<?> updateByStuId(@RequestBody Student student) {
        //1.先查询该学号是否存在
        String selectStuid = selectStuid(student.getStuid());
        //如果有,则进行修改
        if (null != selectStuid) {
            Boolean s = studentService.saveOrUpdate(student);
            return ResultVO.success("修改成功");
        } else {
            return ResultVO.fail("未查询到该学号，请重新输入");
        }
    }

    @GetMapping("/selectAllList")
    @ApiOperation("分页查询所有信息")
    public ResultVO<?> selectAllList(   @RequestParam( defaultValue = "1") int pageNum,
                                        @RequestParam( defaultValue = "10") int pageSize
                                    )
    {
        //利用pagehelper插件获取页面信息pagenum=第几页，pagesize=每页大小
        PageHelper.startPage(pageNum,pageSize);
        List<Student> list= studentService.findAll();
        PageInfo<Student> pageInfo = new PageInfo<Student>(list);

//        PageHelper.startPage(pagenum,pagesize).doSelectPage(()->studentMapper.se)
        return ResultVO.success(pageInfo);
    }

    @GetMapping("/selectOneByStuid")
    @ApiOperation("根据学号查询一条信息")
    public ResultVO<?> selectOneByStuid(String stuid) {
        QueryWrapper<Student> qw = new QueryWrapper<Student>();
        qw.eq("stuId", stuid);
        Student student1 = studentService.getOne(qw);
        if (null != student1) {
            return ResultVO.success(student1);
        }else {
            return ResultVO.fail("未查询到学号");
        }
    }
    @GetMapping("/denglu")
    @ApiOperation("登录功能电话+学号")
    public ResultVO<?> DengLu(@RequestParam String stuid,String phone) {
        QueryWrapper<Student> qw = new QueryWrapper<Student>();
        qw.eq("stuId", stuid);
        qw.eq("phone",phone);
        Student student1 = studentService.getOne(qw);
        List<Student> students = new ArrayList<>();
        students.add(student1);
        if (null != student1) {
            return ResultVO.success(students);
        }else {
            return ResultVO.fail("未查询到学号和电话请先注册");
        }
    }
    public String selectStuid(String stuid) {
        QueryWrapper<Student> qw = new QueryWrapper<Student>();
        qw.eq("stuId", stuid);
        Student student1 = studentService.getOne(qw);
        if (null != student1) {
            return stuid;
        } else {
            return null;
        }
    }
}
